// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function menuover(evt)
{
	evt = evt ? evt : (window.event ? window.event : null);
	var obj = evt.srcElement? evt.srcElement : evt.target;	
	obj.className='menuclick';
}

function resetall()
{
	for (var i=1;i<6;i++){
		var head=window.document.getElementById('head'+i);
		head.className='menuout';
	}
	Element.hide('incumbent_list');
	Element.hide('user_favorites');
	Element.hide('user_info');
	Element.hide('friend_list');
	Element.hide('message_list');
//	Element.hide('report_list');
}

function notIn(val,obj)
{
	var obj_size = obj.length;
	for(i = 0; i < obj_size; i++)
	{
		if(obj[i].value == 0 || obj[i].value == val)return false;
	}
	return true;
}

function addTo(theList,theSelect)
{
	var size1 = theList.length;
	var i = 0;
	i = theList.selectedIndex;
	if (i < 0) return false;

	if(notIn(theList[i].value,theSelect))
	{
		if(theList[i].value == 0)
		{
			theSelect.length = 1;
			theSelect[theSelect.length - 1].text = theList[i].text;
			theSelect[theSelect.length - 1].value = theList[i].value;
		}
		else
		{
			var size = theSelect.length;
			if (theSelect.length >= 6)
			{
				alert("最多不能超过6个选项");
				return false;
			}
			theSelect.length = size + 1;
			theSelect[theSelect.length - 1].text=theList[i].text;
			theSelect[theSelect.length - 1].value=theList[i].value;
		}
        return true;
	}
	return false;
}

function addToWithSubmit(theList,theSelect)
{
	var success = addTo(theList,theSelect);
	if (success == false) return false;

	var selectName = theSelect.name;
	var selectIndex = theList.selectedIndex;

	var i;
	// because addto is success, the length of theSelect is bigger than 0

	// adjust the position of hidden value
	for (i = 1; i < 7; i++)
	{
		var itemValue = document.getElementById(selectName + i);
		if (itemValue.value == "") break;
	}
	var itemValue = document.getElementById(selectName + i);
	itemValue.value = theList[selectIndex].value;
}

function moveWithSubmit(theSelect)
{
	theListId = theSelect.selectedIndex;
	var selectName = theSelect.name;
	remove(theSelect);

	if (theListId >= 0) {
		var i;
		for (i = theListId + 1; i < 6; i++)
		{
			var j=i+1
			document.getElementById(selectName + i).value = document.getElementById(selectName + j).value;
		}
		document.getElementById(selectName + i).value = "";
	}
}

function remove(theSelect)
{
	var theListId = theSelect.selectedIndex;
	if (theListId < 0) return;

	if(theListId >= 0)
	{
		theSelect.remove(theListId);
	}

	if (theListId < theSelect.length)
	{
		theSelect.selectedIndex = theListId;
	}
	else
	if (theSelect.length > 0)
	{
		theSelect.selectedIndex = theSelect.length - 1;
	}
}


function selectIt(){
	var check=0;
    form=document.customize;
    for(var i=0 ;i<form.elements.length;i++){
        if(form.elements[i].type=="checkbox"){
            e=form.elements[i];
            check=!e.checked;
            break;
            }
        }
    for(var i=0 ;i<form.elements.length;i++){
        if(form.elements[i].type=="checkbox"){
            e=form.elements[i];
            e.checked=check;
            }
        }
    }


function openAlert(text,wid,hei) 
{
	Dialog.alert(text,
	{
		windowParameters: {width:wid, height:hei}, okLabel: " "
	});
}

function openConfirm(text,wid,hei,form) 
{
	Dialog.confirm(text,
	{
		windowParameters: {width:wid, height:hei}, okLabel: " ", cancelLabel: " ", ok: function(win) {form.submit();return false;}, cancel: function(win) {return false;}
	});
}

function openAjaxConfirm(text,wid,hei,update,control,action,id,form)
{
	Dialog.confirm(text,
	{
		windowParameters: {width:wid, height:hei}, okLabel: " ", cancelLabel: " ", ok: function(win) {Ajaxupdater(update,control,action,id,form);return false;}, cancel: function(win) {return false;}
	});
}

function Ajaxupdater(update,control,action,id,form)
{
	var path='/'+control+'/'+action+'/'+id
	new Ajax.Updater(update, path, {asynchronous:true, evalScripts:true, parameters:Form.serialize(form)});
}

function drawSBPie(xArray,yArray,domItem)
{
	var hasCanvas = CanvasRenderer.isSupported();
	var arrayLength = xArray.length;
	var xTicks = new Array();
	var yData = new Array();
	for(i = 0; i < arrayLength; i++)
	{
		xTicks[i]={v:i,label:xArray[i]}
		yData[i]=[i,yArray[i]]
	}
	if (arrayLength==1){
		yData[1]=[1,0.0001]
	}
	var options = {
			"colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]),
   			"xTicks": xTicks,
   			"pieRadius": 0.4,
   			"padding": {left: 0, right: 0, top: 0, bottom: 30}
	};  
   if (hasCanvas) {
       var pie = new EasyPlot("pie", options, $(domItem), [yData]);
   }
} 

function drawSBBar(xArray,yArray,domItem)
{
   	var hasCanvas = CanvasRenderer.isSupported();
	var arrayLength = xArray.length;
	var xTicks = new Array();
	var yData = new Array();
	for(i = 0; i < arrayLength; i++)
	{
		xTicks[i]={v:i,label:xArray[i]}
		yData[i]=[i,yArray[i]]
	}
	var options = {
			"colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]),
   			"xTicks": xTicks,
   			"padding": {left: 50, right: 0, top: 0, bottom: 30}
	};
   if (hasCanvas) {
       var bar = new EasyPlot("bar", options, $(domItem), [yData]);
   }
}

function drawSBReport(xArray,yValue1,yValue2,domItem)
{
	drawSBPie(xArray,yValue1,domItem+"pie");
	drawSBBar(xArray,yValue2,domItem+"bar");
}


